﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMeterParamFrame.Model.DIValueConverters
{

    /// <summary>
    /// 时区数据，日时段表数转换类
    /// </summary>
    class Converter_ShiQuAndRiShiDuan : IDIDataConverter
    {

        static uint SHIQU1 = uint.Parse("04010000", System.Globalization.NumberStyles.AllowHexSpecifier);

        static uint MIN_SHIQU1 = uint.Parse("04010001", System.Globalization.NumberStyles.AllowHexSpecifier);
        static uint MAX_SHIQU1 = uint.Parse("04010008", System.Globalization.NumberStyles.AllowHexSpecifier);

        static uint SHIQU2 = uint.Parse("04010000", System.Globalization.NumberStyles.AllowHexSpecifier);

        static uint MIN_SHIQU2 = uint.Parse("04020001", System.Globalization.NumberStyles.AllowHexSpecifier);
        static uint MAX_SHIQU2 = uint.Parse("04020008", System.Globalization.NumberStyles.AllowHexSpecifier);


        public bool CanConverter(string di)
        {
            uint udi = uint.Parse(di, System.Globalization.NumberStyles.AllowHexSpecifier);

            if (udi == SHIQU1 || udi == SHIQU2)
                return true;

            if (udi >= MIN_SHIQU1 && udi <= MAX_SHIQU1)
                return true;

            if (udi >= MIN_SHIQU2 && udi <= MAX_SHIQU2)
                return true;

            return false;
        }

        public string ConvertToUI(string di, string lowValue)
        {
            string newValue = null;
            List<string> datas = new List<string>();
            for (int i = 0; i < lowValue.Length; i += 6)
            {
                datas.Add(lowValue.Substring(i, 6));
                string s = lowValue.Substring(i, 6);
                newValue += s.Substring(0, 2) + "-" + s.Substring(2, 2) + " " + s.Substring(4, 2);
                newValue += ",";
            }
            newValue = newValue.Substring(0, newValue.Length - 1);
            return newValue;
        }

        public string ConvertToLow(string di, string uiValue)
        {
            uint udi = uint.Parse(di, System.Globalization.NumberStyles.AllowHexSpecifier);
            string[] datas = uiValue.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            string value = null;
            foreach (string data in datas)
            {
                string[] values = data.Split(new string[] { "-", " " }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string v in values)
                {
                    value += v.PadLeft(2, '0');
                }
            }
            return value;
        }

        public bool ValidateValue(string di, string value)
        {
            throw new NotImplementedException();
        }
    }
}
